$(window).load(function() {
           
    add_to_cart();
    gototop();

});

function gototop()
{
    $(".gotop").hide();
    $(window).scroll(function() {
        if ($(this).scrollTop() > 10) {
            $('.gotop').fadeIn();
        } else {
            $('.gotop').fadeOut();
        }
    });
    $('.gotop').click(function() {
        $('body,html').animate({scrollTop: 4}, 300);
        return false;
    });
}

function imagebox()
{
    $('.imagebox').fancybox({
         'autoSize' : false
    });
}

function fancyshow(id)
{
    $(".ft-"+id).fancybox({
        loop        : true,
        autoPlay    : true,
        playSpeed   : 4000,
        nextSpeed   : 500,
        prevSpeed   : 500,
        openSpeed   : 500,
        speedOut    : 500,
        openEffect  : "fade", 
        closeEffect : "fade",
        prevEffect	: 'fade', //fade none
        nextEffect	: 'fade', //fade none
        helpers	: {
            title	: {
                type: 'inside'
            },
            thumbs	: {
                width	: 50,
                height	: 50
            }
        }
    });
}


function add_to_cart()
{
    
    $('#addtocart').click(function(){
        id = $('input[name=id]').val();
        addUri = $('input[name=addUri]').val();
        lang = $('input[name=lang]').val();
        $.ajax(
        {
            type:'post',
            url : '/addtocart',
            data:{
                'id' : id
            },
            beforeSend: function(){
                $('#addtocart').prop('disabled', true);
                if(lang == 'vi'){
                    $('#addtocart').html("Đang thêm vào giỏ...");
                }else{
                    $('#addtocart').html("Please wait...");
                }
            },
            success: function(data)
            {
                location.href = addUri;
            }
        });
    });
    
}

function update_cart(id,uri)
{
    var quantity = $('input[name=' + id + ']').val();
    $.ajax(
    {
        type: 'post',
        url: '/updatecart',
        data: {
            'id': id,
            'quantity': quantity,
        },
        success: function()
        {
            location.href = uri;
        }
    });
}

function remove_cart(row_id,uri,lang)
{
    if(lang == 'vi'){
        question = confirm("Bạn có muốn xóa?");
    }else{
        question = confirm("Are you sure you want delete?");
    }
    if (question)
    {
        $.ajax(
        {
            type: 'post',
            url: '/removecart',
            data: {
                //'is-ajax': 1,
                'id': row_id,
            },
            success: function(responseText)
            {
                location.href = uri;
            }
        });
    }
    else
    {
        return false;
    }
}
